home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / print / gs261sr1.zip / GDEVEPSC.C < prev    next >
C/C++ Source or Header  |  1993-05-12  |  14KB  |  460 lines

  1. /* Copyright (C) 1989, 1992 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gdevepsc.c */
  20. /* Epson color dot-matrix printer driver for Ghostscript by dave@exlog.com */
  21. #include "gdevprn.h"
  22.  
  23. /*
  24.  * For 9-pin printers, you may select
  25.  *   X_DPI = 60, 120, or 240
  26.  *   Y_DPI = 60 or 72
  27.  * For 24-pin printers, you may select
  28.  *   X_DPI = 60, 120, 180, 240, or 360
  29.  *   Y_DPI = 60, 72, 180, or 216
  30.  * Note that a given printer implements *either* Y_DPI = 60 | 180 *or*
  31.  * Y_DPI = 72 | 216; no attempt is made to check this here.
  32.  * Note that X_DPI = 180 or 360 requires Y_DPI > 100;
  33.  * this isn't checked either.  Finally, note that X_DPI=240 and
  34.  * X_DPI=360 are double-density modes requiring two passes to print.
  35.  *
  36.  * The values of X_DPI and Y_DPI may be set at compile time:
  37.  * see gdevs.mak.
  38.  * 
  39.  * At some time in the future, we could simulate 24-bit output on
  40.  * 9-pin printers by using fractional vertical positioning;
  41.  * we could even implement an X_DPI=360 mode by using the
  42.  * ESC++ command that spaces vertically in units of 1/360"
  43.  * (not supported on many printers.)
  44.  */
  45.  
  46. #ifndef X_DPI
  47. #  define X_DPI 180            /* pixels per inch */
  48. #endif
  49. #ifndef Y_DPI
  50. #  define Y_DPI 180            /* pixels per inch */
  51. #endif
  52.  
  53. /*
  54. **    Colors for EPSON LQ-2550.
  55. **
  56. **    We map VIOLET to BLUE since this is the best we can do.
  57. */
  58. #define BLACK    0
  59. #define MAGENTA 1
  60. #define CYAN    2
  61. #define VIOLET    3
  62. #define YELLOW    4
  63. #define RED        5
  64. #define GREEN    6
  65. #define WHITE    7
  66.  
  67. /*
  68. **    The offset in this array correspond to
  69. **    the ESC-r n value
  70. */
  71. static char rgb_color[2][2][2] =    {
  72.     BLACK, VIOLET, GREEN,
  73.     CYAN, RED, MAGENTA,
  74.     YELLOW, WHITE,
  75.     };
  76.  
  77. /* Map an RGB color to a printer color. */
  78. #define cv_shift (sizeof(gx_color_value) * 8 - 1)
  79. private gx_color_index
  80. epson_map_rgb_color(gx_device *dev,
  81.   gx_color_value r, gx_color_value g, gx_color_value b)
  82. {
  83. if (gx_device_has_color(dev))
  84.     {
  85. /* use ^7 so WHITE is 0 for internal calculations */
  86.     return (gx_color_index)rgb_color[r >> cv_shift][g >> cv_shift][b >> cv_shift] ^ 7;    
  87.     }
  88. else
  89.     {
  90.     return gx_default_map_rgb_color(dev, r, g, b);
  91.     }
  92. }
  93.  
  94. /* Map the printer color back to RGB. */
  95. private int
  96. epson_map_color_rgb(gx_device *dev, gx_color_index color,
  97.   gx_color_value prgb[3])
  98. {
  99. #define c1 gx_max_color_value
  100. if (gx_device_has_color(dev))
  101.     switch ((ushort)color ^ 7)
  102.         {
  103.         case BLACK:
  104.             prgb[0] = 0; prgb[1] = 0; prgb[2] = 0; break;
  105.         case VIOLET:
  106.             prgb[0] = 0; prgb[1] = 0; prgb[2] = c1; break;
  107.         case GREEN:
  108.             prgb[0] = 0; prgb[1] = c1; prgb[2] = 0; break;
  109.         case CYAN:
  110.             prgb[0] = 0; prgb[1] = c1; prgb[2] = c1; break;
  111.         case  RED:
  112.             prgb[0] = c1; prgb[1] = 0; prgb[2] = 0; break;
  113.         case  MAGENTA:
  114.             prgb[0] = c1; prgb[1] = 0; prgb[2] = c1; break;
  115.         case YELLOW:
  116.             prgb[0] = c1; prgb[1] = c1; prgb[2] = 0; break;
  117.         case  WHITE:
  118.             prgb[0] = c1; prgb[1] = c1; prgb[2] = c1; break;
  119.         }
  120.     else
  121.         return gx_default_map_color_rgb(dev, color, prgb);
  122.     return 0;
  123. }
  124.  
  125. /* The device descriptor */
  126. private dev_proc_print_page(eps_print_page);
  127.  
  128. private gx_device_procs epson_procs =
  129.   prn_color_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close,
  130.     epson_map_rgb_color, epson_map_color_rgb); 
  131.  
  132. gx_device_printer far_data gs_epsonc_device =
  133.   prn_device(epson_procs, "epsonc",
  134.     85,                /* width_10ths, 8.5" */
  135.     110,                /* height_10ths, 11" */
  136.     X_DPI, Y_DPI,
  137.     0, 0, 0.25, 0,        /* margins */
  138.     3, eps_print_page);
  139.  
  140. /* ------ Internal routines ------ */
  141.  
  142. /* Forward references */
  143. private void eps_output_run(P6(byte *, int, int, char, FILE *, int));
  144.  
  145. /* Send the page to the printer. */
  146. #define DD 0x80                /* double density flag */
  147. private int
  148. eps_print_page(gx_device_printer *pdev, FILE *prn_stream)
  149. {    static char graphics_modes_9[5] =
  150.        {    -1, 0 /*60*/, 1 /*120*/, -1, DD+3 /*240*/
  151.        };
  152.     static char graphics_modes_24[7] =
  153.        {    -1, 32 /*60*/, 33 /*120*/, 39 /*180*/,
  154.         -1, -1, DD+40 /*360*/
  155.        };
  156.     int y_24pin = pdev->y_pixels_per_inch > 72;
  157.     int y_mult = (y_24pin ? 3 : 1);
  158.     int line_size = (pdev->width + 7) >> 3;    /* always mono */
  159.     int in_size = line_size * (8 * y_mult);
  160.     byte *in = (byte *)gs_malloc(in_size, 1, "eps_print_page(in)");
  161.     int out_size = ((pdev->width + 7) & -8) * y_mult;
  162.     byte *out = (byte *)gs_malloc(out_size, 1, "eps_print_page(out)");
  163.     int x_dpi = pdev->x_pixels_per_inch;
  164.     char start_graphics =
  165.         (y_24pin ? graphics_modes_24 : graphics_modes_9)[x_dpi / 60];
  166.     int first_pass = (start_graphics & DD ? 1 : 0);
  167.     int last_pass = first_pass * 2;
  168.     int dots_per_space = x_dpi / 10;    /* pica space = 1/10" */
  169.     int bytes_per_space = dots_per_space * y_mult;
  170.     int skip = 0, lnum = 0, pass;
  171. /* declare color buffer and related vars */
  172.     byte *color_in;
  173.     int color_line_size, color_in_size;
  174.     int spare_bits = (pdev->width % 8);    /* left over bits to go to margin */
  175.     int whole_bits = pdev->width - spare_bits;
  176.  
  177.     /* Check allocations */
  178.     if ( in == 0 || out == 0 )
  179.        {    if ( in ) gs_free((char *)in, in_size, 1, "eps_print_page(in)");
  180.         if ( out ) gs_free((char *)out, out_size, 1, "eps_print_page(out)");
  181.         return -1;
  182.        }
  183.  
  184.     /* Initialize the printer and reset the margins. */
  185.     fwrite("\033@\033P\033l\000\033Q\377\033U\001\r", 1, 14, prn_stream);
  186.  
  187. /*    Create color buffer */
  188.     if (gx_device_has_color(pdev))
  189.         {
  190.         color_line_size = gdev_mem_bytes_per_scan_line((gx_device *)pdev);
  191.         color_in_size = color_line_size * (8 * y_mult);
  192.         if((color_in = (byte *)gs_malloc(color_in_size, 1,
  193.             "eps_print_page(color)")) == 0)
  194.             {
  195.             gs_free((char *)in, in_size, 1, "eps_print_page(in)");
  196.             gs_free((char *)out, out_size, 1, "eps_print_page(out)");
  197.             return(-1);
  198.             }
  199.         }
  200.     else
  201.         {
  202.         color_in = in;
  203.         color_in_size = in_size;
  204.         color_line_size = line_size;
  205.         }
  206.  
  207.     /* Print lines of graphics */
  208.     while ( lnum < pdev->height )
  209.        {
  210.         int lcnt;
  211.         byte *nextcolor = NULL;    /* position where next color appears */
  212.         byte *nextmono = NULL;    /* position to map next color */
  213.  
  214.         /* Copy 1 scan line and test for all zero. */
  215.         gdev_prn_copy_scan_lines(pdev, lnum, color_in, color_line_size);
  216.  
  217.         if ( color_in[0] == 0 &&
  218.              !memcmp((char *)color_in, (char *)color_in + 1, color_line_size - 1)
  219.            )
  220.            {    lnum++;
  221.             skip += 3 / y_mult;
  222.             continue;
  223.            }
  224.  
  225.         /* Vertical tab to the appropriate position. */
  226.         while ( skip > 255 )
  227.            {    fputs("\033J\377", prn_stream);
  228.             skip -= 255;
  229.            }
  230.         if ( skip )
  231.             fprintf(prn_stream, "\033J%c", skip);
  232.  
  233.         /* Copy the rest of the scan lines. */
  234.         lcnt = 1 + gdev_prn_copy_scan_lines(pdev, lnum + 1, 
  235.             color_in + color_line_size, color_in_size - color_line_size);
  236.  
  237.         if ( lcnt < 8 * y_mult )
  238.             {
  239.             memset((char *)(color_in + lcnt * color_line_size), 0,
  240.                 color_in_size - lcnt * color_line_size);
  241.             if (gx_device_has_color(pdev))    /* clear the work buffer */
  242.                 memset((char *)(in + lcnt * line_size), 0,
  243.                     in_size - lcnt * line_size);
  244.             }
  245.             
  246. /*
  247. **    We need to create a normal epson scan line from our color scan line
  248. **    We do this by setting a bit in the "in" buffer if the pixel byte is set
  249. **    to any color.  We then search for any more pixels of that color, setting
  250. **    "in" accordingly.  If any other color is found, we save it for the next
  251. **    pass.  There may be up to 7 passes.
  252. **    In the future, we should make the passes so as to maximize the
  253. **    lif